package com.redteam.xomtro;

import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.MenuItem;
import android.view.View;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

/**
 * Created by AnhThuanIT on 5/21/2014.
 */
public class MasterActivity extends ActionBarActivity implements View.OnClickListener{
    public static final int ACTION_BAR_CUSTOM_OVERLAY = 0;
    public static final int ACTION_BAR_CHILD = 1;
    public static final int ACTION_BAR_CHILD_OVERLAY = 2;

    public ImageLoader imageLoader;
    public DisplayImageOptions options;
    public DisplayImageOptions optionsAvatar;
    public Typeface mainFont;
    protected ActionBar actionBar;
    public Typeface fontSLight;
    public Typeface fontSBold;
    protected FragmentManager fragmentManager;
    private int actionBarMode = ACTION_BAR_CHILD;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        switch (actionBarMode) {
            case ACTION_BAR_CUSTOM_OVERLAY:
                actionBar.setDisplayShowHomeEnabled(false);
                actionBar.setDisplayShowCustomEnabled(true);
                //actionBar.setBackgroundDrawable(getResources().getDrawable(R.color.welcome_actionbar_bg));
                break;
            case ACTION_BAR_CHILD:
                actionBar.setDisplayShowHomeEnabled(true);
                actionBar.setHomeButtonEnabled(true);
                actionBar.setHomeAsUpIndicator(R.drawable.abc_ic_ab_back_holo_dark);
                actionBar.setDisplayShowTitleEnabled(true);
                //actionBar.setIcon(R.drawable.ic_menu_about);
                break;
            case ACTION_BAR_CHILD_OVERLAY:
                actionBar.setDisplayShowHomeEnabled(true);
                actionBar.setHomeButtonEnabled(true);
                actionBar.setHomeAsUpIndicator(R.drawable.abc_ic_ab_back_holo_dark);
                actionBar.setDisplayShowTitleEnabled(true);
               // actionBar.setBackgroundDrawable(getResources().getDrawable(R.color.map_actionbar_bg));
               // actionBar.setIcon(R.drawable.ic_menu_about);
                break;
        }

        mainFont = Typeface.createFromAsset(getAssets(), "fonts/solgas.ttf");
        fontSLight = Typeface.createFromAsset(getAssets(), "fonts/segoeui_sl.ttf");
        fontSBold = Typeface.createFromAsset(getAssets(), "fonts/segoeui_sb.ttf");
        imageLoader = ImageLoader.getInstance();
        imageLoader.init(ImageLoaderConfiguration.createDefault(this));
        imageLoader.handleSlowNetwork(true);
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.im_default_small)
                .showImageOnFail(R.drawable.im_default)
                .cacheInMemory(true)
                .cacheOnDisc(true)
                .considerExifParams(true)
                .build();
        optionsAvatar = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.im_avatar)
                .showImageOnFail(R.drawable.im_avatar)
                .cacheInMemory(true)
                .cacheOnDisc(true)
                .considerExifParams(true)
                .build();
        fragmentManager = getSupportFragmentManager();
    }

    public void beginTransaction(Fragment fragment) {
        imageLoader.stop();
        fragmentManager.beginTransaction()
                .setCustomAnimations(R.anim.fade_in, R.anim.fade_out)
                .replace(R.id.container, fragment)
                .commit();
    }

    @Override
    public void onClick(View view) {
    }

    protected void setActionBarMode(int actionBarMode) {
        this.actionBarMode = actionBarMode;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

}
